package xyz.hujf.tool.autoclass;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author Hujf
 * @title: AtomicInteger
 * @date 2021-04-2511:15
 * @description: 一个原子操作 也是实现了cas的非阻塞式原子性
 */
public class AtomicIntegerTest {

    private static int num = 0;
    private static CountDownLatch count = new CountDownLatch(10);

    public static void main(String[] args) throws InterruptedException {
        AtomicInteger atomicInteger = new AtomicInteger();
        for (int i = 0; i < 10; i++) {
            new Thread(() -> {
                for (int j = 0; j < 1000; j++) {
                    inceament();
                    atomicInteger.getAndIncrement();
                }
                count.countDown();
            }).start();

        }
        count.await();
        System.out.println(num);
        System.out.println("atomicInteger.intValue() = " + atomicInteger.get());
    }

    private synchronized static void inceament() {
        num++;
    }
}
